package com.heima.admin.job;

import com.heima.admin.service.WemediaNewsAutoScanService;
import com.heima.feigns.WemediaFeign;
import com.heima.model.common.dtos.ResponseResult;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class WeMediaNewsAutoScanJob {

    @Autowired
    WemediaFeign wemediaFeign;

    @Autowired
    WemediaNewsAutoScanService wemediaNewsAutoScanService;

    @XxlJob("WeMediaNewsAutoScanJob")
    public ReturnT autoScanJob(String params) {
        log.info("===================自动发布文章任务 触发  start  ====================");
        ResponseResult<List<Integer>> release = wemediaFeign.findRelease();
        if (release.getCode().intValue() != 0) {
            log.error("远程调用待文章接口 失败 原因 ：{}", release.getErrorMessage());
            return ReturnT.FAIL;
        }
        List<Integer> newsIds = release.getData();
        log.info("需要发布的文章id ==>{}",newsIds);
        if (newsIds != null && newsIds.size() > 0) {
            for (Integer newsId : newsIds) { //发布
                wemediaNewsAutoScanService.autoScanByMediaNewsId(newsId);
            }
        }

        log.info("===================自动发布文章任务 结束  end  ====================");
        return ReturnT.SUCCESS;
    }
}
